//
//  BNDOfficeDocPopView.m
//  OfficeDoc
//
//  Created by pie on 2022/3/31.
//  Copyright © 2022 pie. All rights reserved.
//

#import "BNDOfficeDocPopView.h"
#import "UIButton+Convenient.h"
#import "BNDOfficeDocModel.h"

@interface BNDOfficeDocPopView()

@property (nonatomic,  copy) VoidBlock_id popBlock;
@property (nonatomic,  copy) VoidBlock_id cancleBlock;
@property (nonatomic, strong) UIView    *backView;
@property (nonatomic, strong) UIView    *transparentView;
@property (nonatomic, strong) UIView    *baseView;
@property (nonatomic, strong) UILabel * titleLabel;
@property (nonatomic, strong) UILabel * subtitleLabel;
@property (nonatomic, strong) UIButton * checkButton;
@property (nonatomic, strong) UIButton * closeButton;
@property (nonatomic, strong) UILabel * timeLabel;

@property (nonatomic, assign) CGFloat   kAlertWidth;
@property (nonatomic, assign) CGFloat   kAlertHeight;
@property (nonatomic, assign) NSInteger  timeCount;

@property(nonatomic,  strong) RACDisposable * dispoable;

@property(nonatomic,  strong) BNDOfficeDocModel *officeDocModel;


@end

@implementation BNDOfficeDocPopView

- (instancetype)initWithTitle:(id)data
                  clickAction:(VoidBlock_id)actionHandler
                 cancleAction:(VoidBlock_id)cancleHandler;
{
    self = [super init];
    if (self) {
        
        _officeDocModel = data;
        _kAlertWidth = 335;
        _kAlertHeight = 436;
        _timeCount = _officeDocModel.seconds;
        
        self.popBlock = actionHandler;
        self.cancleBlock = cancleHandler;
        self.backgroundColor = UIColorHexAndAlpha(0x000000, 0.6);
        
        [self addSubview:self.backView];
        [self addSubview:self.transparentView];
        
        UIImageView *bannarImageView = [[UIImageView alloc] initWithImage:BNDImageNamed(@"bnd_tanchaugn_top_banner")];
        bannarImageView.frame = CGRectMake(0, 0, _kAlertWidth, 210);
        [self.transparentView addSubview:bannarImageView];
        
        
        [self.transparentView addSubview:self.titleLabel];
        self.titleLabel.text = self.officeDocModel.title;
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.transparentView);
            make.top.mas_equalTo(bannarImageView.mas_bottom).offset(20);
        }];
        
        [self.transparentView addSubview:self.subtitleLabel];
        self.subtitleLabel.text = self.officeDocModel.popUpContent;
        [_subtitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.transparentView.mas_left).offset(20);
            make.right.mas_equalTo(self.transparentView.mas_right).offset(-20);
            make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(17);
        }];
        
        [self.transparentView addSubview:self.checkButton];
        [self.checkButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.transparentView);
            make.size.mas_equalTo(CGSizeMake(200, 44));
            make.bottom.mas_equalTo(self.transparentView.mas_bottom).offset(-28);
        }];
        
        [self.backView addSubview:self.closeButton];
        [self.closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.transparentView.mas_right);
            make.size.mas_equalTo(CGSizeMake(32, 25));
            make.bottom.mas_equalTo(self.transparentView.mas_top).offset(-11);
        }];
        
        [self.backView addSubview:self.timeLabel];
        self.timeLabel.text = [NSString stringWithFormat:@"%lds后可关闭",(long)_officeDocModel.seconds];
        [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.closeButton.mas_left).offset(-10);
            make.size.mas_equalTo(CGSizeMake(80, 24));
            make.centerY.mas_equalTo(self.closeButton);
        }];
        
        @weakify(self)
        self.dispoable = [[RACSignal interval:1.0 onScheduler:[RACScheduler mainThreadScheduler]] subscribeNext:^(NSDate * _Nullable x) {
            @strongify(self)
            if (self.timeCount == 0) {
                [self.dispoable dispose];
                self.timeLabel.hidden = YES;
                return;
            }
            self.timeCount --;
            self.timeLabel.text = [NSString stringWithFormat:@"%lds后可关闭",self.timeCount];
        }];
        
        self.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
    }
    return self;
}

- (void)show:(UIView*)displayView
{
    self.frame = displayView.frame;
    _backView.frame = self.frame;
    [displayView addSubview:self];
    self.backView.alpha = 0;
    self.baseView = displayView;;
    [UIView animateWithDuration:0.35f delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        self.backView.alpha = 1;
    } completion:^(BOOL finished) {
        
    }];
}

- (void)clickCheckMethod
{
    [self.dispoable dispose];
    self.timeLabel.hidden = YES;
    _timeCount = 0;
    BND_BLOCK_EXEC(self.popBlock,_officeDocModel);
    [self removePopoverView];
   
}

- (void)removePopoverView
{
    if(_timeCount ==0){
        [self popWindosOff];
        [self.dispoable dispose];
        self.backView.alpha = 1;
        BND_dispatch_main_sync_safe(^{
            BND_BLOCK_EXEC(self.cancleBlock,self->_officeDocModel);
        });
        [UIView animateWithDuration:0.35f delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            self.backView.alpha = 0;
        } completion:^(BOOL finished) {

            [self removeFromSuperview];
            
        }];
    }
}

- (void)popWindosOff
{
    [BNDApiManager officeDocRequestPopWindosOff:self.officeDocModel.recordId success:^{
        
    } failure:^(NSError * _Nullable error) {
        
    }];
}


#pragma mark - Get

- (UIView*)backView
{
    if(!_backView)
    {
        _backView = [[UIView alloc] init];
        _backView.backgroundColor = [UIColor clearColor];
    }
    return _backView;
}

- (UIView*)transparentView
{
    if(!_transparentView){
        _transparentView = [[UIView alloc] init];
        _transparentView.backgroundColor = [UIColor whiteColor];
        _transparentView.layer.cornerRadius = 16.0;
        _transparentView.frame = CGRectMake((kDeviceWidth-_kAlertWidth)/2, (kDeviceHeight-_kAlertHeight)/2, _kAlertWidth,_kAlertHeight);
    }
    return _transparentView;
}

- (UILabel*)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = BNDPFMediumFont(22);
        _titleLabel.textColor = UIColor.blackColor;
        _titleLabel.text = @"公告标题";
        _titleLabel.numberOfLines = 1;
    }
    return _titleLabel;
}

- (UILabel*)subtitleLabel
{
    if (!_subtitleLabel) {
        _subtitleLabel = [[UILabel alloc] init];
        _subtitleLabel.font = BNDPFRegularFont(15);
        _subtitleLabel.textColor = UIColorWithHex(0x757A82);
        _subtitleLabel.text = @"显示公告概述内容根据公告内容变化内容最多可显示3行最多可显示3行最多容最多可显示3行最多可显示3行最多";
        _subtitleLabel.numberOfLines = 2;
        _subtitleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _subtitleLabel;
}

- (UIButton *)checkButton
{
    if(!_checkButton){
        _checkButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_checkButton lk_setTitle:@"查看详情"];
        _checkButton.layer.cornerRadius = 22;
        _checkButton.layer.masksToBounds = YES;
        _checkButton.backgroundColor = UIColorWithHex(0x3770EB);
        [_checkButton lk_addTarget:self action:@selector(clickCheckMethod)];
    }
    return _checkButton;
}

- (UIButton *)closeButton
{
    if(!_closeButton){
        _closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_closeButton lk_setImage:BNDImageNamed(@"bnd_delete_pop")];
        [_closeButton lk_addTarget:self action:@selector(removePopoverView)];
    }
    return _closeButton;
}

- (UILabel*)timeLabel
{
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.font = BNDPFRegularFont(12);
        _timeLabel.textColor = UIColorWithHex(0xFFFFFF);
        _timeLabel.text = @"3s后可关闭";
        _timeLabel.numberOfLines = 1;
        _timeLabel.textAlignment = NSTextAlignmentCenter;
        _timeLabel.backgroundColor = UIColorHexAndAlpha(0x070707, 0.6);
        _timeLabel.layer.cornerRadius = 12;
        _timeLabel.layer.masksToBounds = YES;
    }
    return _timeLabel;
}

@end
